#pragma once
#include <widgets/SPopupWindow.h>
#include <vector>
#include <QListWidget>
#include <core/StyleMgr.h>
#include <widgets/STabCtrl.h>
#include <qbprotocol/include/SSQBModel.h>
#include <QDoubleSpinBox>
class SCheckEx;
class SRadioGroup;
class STabCtrl;
class QScrollArea;
class QDoubleSpinBox;

enum eBondType {
	BOND_GOV = 0,
	BOND_CREDIT,
	BOND_NCDPrimary,
	BOND_NCDTrade //NCD二级成交
};

enum eType {
	eType_PreClose = 0,
	eType_CDC,
	eType_Num
};

class DoubleSpinBox : public QDoubleSpinBox
{
	Q_OBJECT
public:
	DoubleSpinBox(QWidget* p,bool bLeftSpinBox);
	void setStatusError(bool bStatusError) { m_bStatusError = bStatusError; }
	bool getStatusError()const			   { return m_bStatusError; }
	int			  m_index = 0;
	bool		  m_bLeftSpinBox = true;
	bool		  m_bStatusError = false;
protected:
	void wheelEvent(QWheelEvent* event);
};

class TermSettingLabel : public QLabel 
{
	Q_OBJECT
public:
	TermSettingLabel(QWidget* p, QString term, QString leftUnit, QString rightUnit, bool haveDoubleSpinBox,QString leftNum = "", QString rightNum = "");
	bool isSelected() {	return m_selectState != Qt::Unchecked;}
	void setSelectState(Qt::CheckState v, bool notify);
	void setIndex(int index);
	void setSpinBoxText(const char* leftValue,const char* rightValue);
	QString getSpinBoxText(bool bLeftSpinBox) const;
	DoubleSpinBox* getLeftSpinBox()  { return m_doubleSpinBoxLeft; }
	DoubleSpinBox* getRightSpinBox() { return m_doubleSpinBoxRight;}
	bool bhaveDobleSpinBox() { return m_haveDoubleSpinBox; }
Q_SIGNALS:
	void stateChanged(int);
protected:
	void paintEvent(QPaintEvent* event) override;
	virtual void mousePressEvent(QMouseEvent*) override;
	virtual void enterEvent(QEvent* event) override;
	virtual void leaveEvent(QEvent* event) override;
private:
	QString			m_term; 
	QString			m_leftDateUnit;
	QString			m_rightDateUnit;
	QString			m_leftNum;
	QString			m_rightNum;
	int				m_index = -1;
	Qt::CheckState	m_selectState = Qt::Unchecked;
	bool			m_hover = false;
	bool			m_haveDoubleSpinBox = true;
	bool			m_bSelected = false;
	QRect			m_indicatorRect;
	DoubleSpinBox*  m_doubleSpinBoxLeft = nullptr;
	DoubleSpinBox*	m_doubleSpinBoxRight = nullptr;

};

class TermSettingArea : public QWidget
{
	Q_OBJECT
public:
	TermSettingArea(QWidget* p, eBondType type);
	std::vector<TermSettingLabel*> m_vAreaLabels;
private:
	eBondType	m_type;
	TermSettingLabel* termLabel = nullptr;
};

//期限设置
class MatrixTermSetting : public QWidget
{
	Q_OBJECT
public:
	enum intRangeEnum {
		ID_CHK_1Y = 100101,
		ID_CHK_2Y,
		ID_CHK_3Y,
		ID_CHK_5Y,
		ID_CHK_7Y,
		ID_CHK_10Y,
		ID_CHK_15Y,
		ID_CHK_20Y,
		ID_CHK_30Y,
		ID_CHK_50Y,
		ID_CHK_LONGTERM
	}; //利率债
	MatrixTermSetting(QWidget* p,int bondType,xQBBondDealTermRgReq* req);
	void setTabType(int nType);
	void saveConfig(bool binitData = false);
	void saveRangeInfo();
	bool checkSaveReqValid();
	bool checkSaveReqValid(QString& strTip);
protected:
	void initData();
	void initRangeInput();
	void setRangeValue(int rank,const char* leftValue,const char* rightValue);
	void changeRangeErrorStatus();
	int  getConfig(std::vector<UINT>& m_RangeData,const int& rangeType) const;
	QString getRangeValue(int rank, QString strBefore,bool bLeftSpinBox) const;
	void changeTermRangeText();
	void resetRangePairStatus(int rank);
	void resetTipShowStatus();
	void updateTipPos(int rank,bool bleft);
	void createLabelCheckedConnect();
	void createLabelSpinBoxEditConnect();
	bool noChooseErrorCheck(int RangeType, int RangeNo) const;
	TermSettingArea* getTabTermSettingArea(int tabType) const;
Q_SIGNALS:
	void notifyTermChanged(int type);
protected slots:
	void onChangeBondTab(int index);
	void onCheckedBondLabel(int index);
	void onEditedBondLabelSpinBox();
private:
	TermSettingArea*	m_bondGovLabelArea = nullptr;
	QScrollArea*		m_bondGovScrollArea = nullptr;
	TermSettingArea*	m_bondCreditLabelArea = nullptr;
	QScrollArea*		m_bondCreditScrollArea = nullptr;
	TermSettingArea*	m_bondNCDLabelArea = nullptr;
	QScrollArea*		m_bondNCDScrollArea = nullptr;
	STabCtrl*			m_tabType = nullptr;
	xQBBondDealTermRgReq*	m_req = nullptr;
	int						m_tabCurrent;
	int						m_IntData[11];
	int						m_CretData[10];
	int						m_NCDData[7];
	int						m_nParentMode;
	int						m_tipRank = -1;
	bool					m_bGovBondLabelConnect = false;
	bool					m_bCreditBondLabelConnect = false;
	bool					m_bNCDTradeBondLabelConnect = false;
	bool					m_bGovBondSpinBoxConnect = false;
	bool					m_bCreditBondSpinBoxConnect = false;
	bool					m_bNCDTradeBondSpinBoxConnect = false;
	bool					m_errType = 0; //0:请保证左边期限小于右边期限 1:右侧期限不应大于下一级左侧期限
};

class upDownBpArea : public QWidget
{
public:
	upDownBpArea(QWidget* p);
	SRadioGroup* getSRadioGroup() { return m_rg; }
private:
	SRadioGroup* m_rg = nullptr;
	SCheckEx* m_preClose = nullptr;
	SCheckEx* m_CDC = nullptr;
};

//涨跌bp
class MatrixUpDownBp : public QWidget
{
	Q_OBJECT
public:
	MatrixUpDownBp(QWidget* p, int bondType,xQBBondDealTermRgReq* req);
	upDownBpArea* getCurrentTypeArea(int type) const;
	void setTabType(int index);
protected:
	void createSCheckExConnect();
	void setSCheckExType();
protected slots:
	void onChangeBondTab(int index);
	void onRatioClick(int index);
private:
	xQBBondDealTermRgReq*	m_req = nullptr;
	STabCtrl*		m_tabType = nullptr;
	int				m_tabCurrent;
	upDownBpArea*	m_bondGOVUpDownArea = nullptr;
	upDownBpArea*	m_bondCreditUpDownArea = nullptr;
	upDownBpArea*	m_bondNCDUpDownArea = nullptr;
	bool			m_bGovSCheckedConnect = false;
	bool			m_bCreditSCheckedConnect = false;
	bool			m_bNCDTradeSCheckedConnect = false;
};

//数据展示设置
class MatrixDataDisplaySetting : public QWidget
{
	Q_OBJECT
public:
	enum eType {
		eType_Ytm = 0,
		eType_bps,
		eType_HeatMap,
		eType_HorizontalBarChart,
		eType_VerticalBarChart,
		eType_Sum 
	};
	MatrixDataDisplaySetting(QWidget* p, xQBBondDealTermRgReq* req);
	void setCurrentType();
	void initSCheckEx(int index);
protected slots:
	void onRatioClick(int index);
private:
	SRadioGroup*	m_textShowRGroup = nullptr;
	SRadioGroup*	m_chartShowRGroup = nullptr;
	SCheckEx*		m_yield = nullptr;
	SCheckEx*		m_bps = nullptr;
	SCheckEx*		m_heatMap = nullptr;
	SCheckEx*		m_horizontalBarChart = nullptr;
	SCheckEx*		m_verticalBarChart = nullptr;
	xQBBondDealTermRgReq*  m_req = nullptr;
};

class BondDealMatrixSettingDlg : public SPopupWindow
{
	Q_OBJECT
public:
	enum LeftItem {
		TermSetting = 0,
		UpDownBp,
		DataDisplaySetting,
		ItemNum
	};
	enum emParentMode {
		emParent_BondDeal = 0,
		emParent_FixedIncome
	};
	BondDealMatrixSettingDlg(QWidget* parent, int bondType, xQBBondDealTermRgReq* req,int nParentMode = emParent_BondDeal);
protected:
	void paintEvent(QPaintEvent* event) override;
	void changePage(int index);
	void changeReqParam();
	void saveConfig(int textShowType, int chartShowType);
Q_SIGNALS:
	void notifyMatrixTermChanged(int type);
protected slots:
	void onListClick(QListWidgetItem* item);
	void onApplyChange();
	void onNotifyTermChange(int type);
private:
	QListWidget*				m_leftList = nullptr;
	MatrixTermSetting*			m_termSetting = nullptr;
	MatrixUpDownBp*				m_upDownBp = nullptr;
	MatrixDataDisplaySetting*	m_dataDisplaySetting = nullptr;
	int							m_bondType;
	xQBBondDealTermRgReq*		m_req = nullptr;
	int							m_nParentMode;
};